﻿package cn.edu.zjusc.Webservice;

import javax.ejb.Stateless;
import javax.jws.*;
import javax.jws.soap.SOAPBinding;
import javax.persistence.*;

import java.util.List;

import org.jboss.wsf.spi.annotation.WebContext;

import cn.edu.zjusc.POJO.*;
import cn.edu.zjusc.Security.*;
import cn.edu.zjusc.Util.PersistenceHelper;

/**
 * 验证用户的Service
 * 
 * @author Tr0j4n
 * 
 */
@Stateless
@WebService(name = "IdentifyService", serviceName = "IdentifyService")
@SOAPBinding(style = SOAPBinding.Style.RPC)
@WebContext(contextRoot = "/ZJUSCIX/Service", urlPattern = "/IdentifyService")
public class IdentifyService {
	
	/**
	 * 验证用户登陆
	 * @param userName
	 * @param password
	 * @return 识别信息
	 */
	@SuppressWarnings({ "rawtypes" })
	@WebMethod
	public LoginResponse IdentifyMySelf(
			@WebParam(name = "uname") String userName,
			@WebParam(name = "pwd") String password) {

		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("zjusc");
		EntityManager em = emf.createEntityManager();

		LoginResponse outputResult = null;

		// 开始学生查询
		Query queryStudent = em
				.createQuery("from StudentExtendInfo s where UserName=?1 and Password=?2");
		queryStudent.setParameter(1, userName);
		queryStudent.setParameter(2, password);				

		List studentList = queryStudent.getResultList();
		// 这个人是学生
		if (studentList.size() > 0) {
			outputResult = IdentifyStudent((StudentExtendInfo) studentList
					.get(0));
		}
		// 这个不是学生
		else {
			Query queryTeacher = em
					.createQuery("from OnlineRegisterAdmin  s where UserName=?1 and Password=?2");
			queryTeacher.setParameter(1, userName);
			queryTeacher.setParameter(2, password);		
			
			List teacherList = queryTeacher.getResultList();
			// 这个人是老师
			if (teacherList.size() > 0) {
				outputResult = IdentifyTeacher((OnlineRegisterAdmin) teacherList
						.get(0));
			} else {
				// 三无人员
				LoginResponse info = new LoginResponse();
				info.setUserType(0);
				info.setAuthCode("");
				outputResult = info;
			}
		}
		em.close();
		emf.close();
		return outputResult;
	}

	/**
	 * 验证学生身份
	 * 
	 * @return
	 */
	LoginResponse IdentifyStudent(StudentExtendInfo sei) {
		LoginResponse info = new LoginResponse();
		info.setUserType(1);
		String authCode = Authentication.addUserLogin(sei.getStuext_userName(),1);
		info.setAuthCode(authCode);
		info.setStuApplyID(sei.getStuext_id());
		//加入登陆日志
		LogInfo log=new LogInfo("学号 "+sei.getStuext_id()+" 使用用户名 "+sei.getStuext_userName()+" 登陆系统","1");
		PersistenceHelper.addEntity(log);
		
		return info;
	}

	/**
	 * 验证管理员身份
	 * 
	 * @return
	 */
	LoginResponse IdentifyTeacher(OnlineRegisterAdmin ora) {
		LoginResponse info = new LoginResponse();
		info.setUserType(100);
		String authCode = Authentication.addUserLogin(ora.getUsername(),100);
		info.setAuthCode(authCode);
		return info;
	}

	/**
	 * 验证学生用户名是否被占用
	 * @param userName
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	@WebMethod
	public boolean IdentifyStudentUserNameAvaliable(
			@WebParam(name = "uname") String userName) {

		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("zjusc");
		EntityManager em = emf.createEntityManager();

		Query queryStudent = em
				.createQuery("from StudentExtendInfo s where UserName=?1");
		queryStudent.setParameter(1, userName);

		List studentList = queryStudent.getResultList();

		if (studentList.size() > 0) {
			return false;
		}
		return true;

	}

	/**
	 * 验证管理员用户名是否被占用
	 * @param userName
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	@WebMethod
	public boolean IdentifyTeacherUserNameAvaliable(
			@WebParam(name = "uname") String userName) {

		EntityManagerFactory emf = Persistence
				.createEntityManagerFactory("zjusc");
		EntityManager em = emf.createEntityManager();
		
		Query queryTeacher = em
				.createQuery("from OnlineRegisterAdmin  s where UserName=?1");
		queryTeacher.setParameter(1, userName);
		List teacherList = queryTeacher.getResultList();
		if (teacherList.size() > 0) {
			return false;
		}
		return true;
	}
}
